package client

import (
	"github.com/golang/glog"
	"github.com/oikomi/FishChatServer2/protocol/rpc"
	"github.com/oikomi/FishChatServer2/server/access/conf"
	sd "github.com/oikomi/FishChatServer2/service_discovery/etcd"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
)

type HallRPCCli struct {
	conn *grpc.ClientConn
}

func NewHallRPCCli() (hallRPCCli *HallRPCCli, err error) {
	r := sd.NewResolver(conf.Conf.RPCClient.HallClient.ServiceName)
	b := grpc.RoundRobin(r)
	conn, err := grpc.Dial(conf.Conf.RPCClient.HallClient.EtcdAddr, grpc.WithInsecure(), grpc.WithBalancer(b))
	if err != nil {
		glog.Error(err)
		panic(err)
	}
	hallRPCCli = &HallRPCCli{
		conn: conn,
	}
	return
}

func (hallRPCCli *HallRPCCli) Login(loginReq *rpc.HallLoginReq) (res *rpc.HallLoginRes, err error) {
	l := rpc.NewHallRPCClient(hallRPCCli.conn)
	if res, err = l.Login(context.Background(), loginReq); err != nil {
		glog.Error(err)
	}
	return
}

func (hallRPCCli *HallRPCCli) CreateTable(uid int64) (res *rpc.CreateTableRes, err error) {
	Req := &rpc.CreateTableReq{
		UID: uid,
	}
	l := rpc.NewHallRPCClient(hallRPCCli.conn)
	if res, err = l.CreateTable(context.Background(), Req); err != nil {
		glog.Error(err)
	}
	return
}

func (hallRPCCli *HallRPCCli) EnterTable(uid int64, tableID uint32) (res *rpc.EnterTableRes, err error) {
	Req := &rpc.EnterTableReq{
		UID:     uid,
		TableID: tableID,
	}
	l := rpc.NewHallRPCClient(hallRPCCli.conn)
	if res, err = l.EnterTable(context.Background(), Req); err != nil {
		glog.Error(err)
	}
	return
}
